package com.baidu.customview.widget;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuff.Mode;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import com.baidu.customview.R;
import com.baidu.customview.utils.MetricsUtils;

/**
 * Created by imagine on 2015/11/14.
 */
public class PorterDuffColorFilter extends View{
    private Paint p;
    private int left, top;
    private Bitmap bitmap;
    public PorterDuffColorFilter(Context context) {
        super(context);
    }

    public PorterDuffColorFilter(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        p=new Paint(Paint.ANTI_ALIAS_FLAG);
        p.setStyle(Paint.Style.FILL);
        p.setColorFilter(new android.graphics.PorterDuffColorFilter(getResources().getColor(R.color.red), Mode.ADD));

        bitmap= BitmapFactory.decodeResource(getResources(), R.mipmap.peien);
        left= (MetricsUtils.winMetrics(context).widthPixels-bitmap.getWidth())/2;
        top= (MetricsUtils.winMetrics(context).heightPixels-bitmap.getHeight())/2;
    }

    public PorterDuffColorFilter(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public PorterDuffColorFilter(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bitmap,left,top,p);
    }
}
